﻿using System;
using System.Collections.Generic;
using System.Text;

namespace UtilZ.Dotnet.Ex.Communication.Net.Web
{
    /// <summary>
    /// api方法返回文本值
    /// </summary>
    public class ApiTextResult : ApiActionResultAbs
    {
        private string _contentType = null;
        /// <summary>
        /// http响应内容mime类型
        /// </summary>
        public string ContentType
        {
            get { return _contentType; }
            set { _contentType = value; }
        }

        private string _content = null;
        /// <summary>
        /// 文本内容
        /// </summary>
        public string Content
        {
            get { return _content; }
            set { _content = value; }
        }




        /// <summary>
        /// 构造函数
        /// </summary>
        public ApiTextResult()
            : base()
        {

        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="content">文本内容</param>
        public ApiTextResult(string content)
            : base()
        {
            this._content = content;
        }


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="contentType">http响应内容mime类型</param>
        /// <param name="content">文本内容</param>
        public ApiTextResult(string contentType, string content)
            : base()
        {
            this._contentType = contentType;
            this._content = content;
        }



        /// <summary>
        /// 设置响应值
        /// </summary>
        /// <param name="request">http请求</param>
        /// <param name="response">http响应</param>
        protected override void PrimitiveSetResponse(HttpRequest request, HttpResponse response)
        {
            byte[] buufer;
            if (this._content == null)
            {
                buufer = new byte[0];
            }
            else
            {
                buufer = request.GetClientTextEncoding().GetBytes(this._content);
            }

            string contentType = this._contentType;
            if (string.IsNullOrWhiteSpace(contentType))
            {
                contentType = HttpContentTypes.TEXT;
            }

            response.SetBytes(contentType, buufer);
        }
    }
}
